/**
 * @Title: WxApiUtil
 * @Package cn.com.yiqi.wsc.util
 * @Description: TODO
 * Copyright: Copyright (c) 2016
 * Company: 成都壹柒互动科技有限公司
 * @author 会员系统组-林威
 * @date 2017/3/17 19:04
 * @version V1.0
 */
package cn.com.yiqi.wsc.util;

import org.apache.commons.lang.StringUtils;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 *@ClassName: WxApiUtil
 * Description: TODO
 * @author 会员系统组-林威
 * @date 2017/3/17 19:04
 *
 */
public class WxApiUtil {
    /**
     * 对象转xml字符串
     * @param obj
     * @return
     */
    public static String objToXml(Object obj) {
        Field[] fields = obj.getClass().getDeclaredFields();
        StringBuilder sb = new StringBuilder();
        if (fields.length > 0) {
            sb.append("<xml>");
            for (Field f:
                    fields) {
                f.setAccessible(true);
                String name = f.getName();
                try {
                    Object value = f.get(obj);
                    if (null != value) {
                        sb.append(String.format("<%s>%s</%s>", name, value.toString(), name));
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            sb.append("</xml>");
        }
        return sb.toString();
    }

    /**
     * xml转obj
     * @param xml
     * @return
     */
    public static <T> T xmlToObj(String xml, Class<T> clazz) {
        T obj = null;
        try {
            obj = clazz.newInstance();
            String reg = "<[A-Za-z0-9][^>]+>";
            Pattern pattern = Pattern.compile(reg);
            Matcher matcher = pattern.matcher(xml);
            while (matcher.find()) {
                String name = matcher.group();
                int index = xml.indexOf(name) + name.length();
                if (xml.startsWith("<![CDATA[", index)) {
                    int i = xml.indexOf("]]>", index);
                    String value = xml.substring(index + 9, i);
                    try {
                        Field f = clazz.getDeclaredField(name.replace("<", "").replace(">", ""));
                        f.setAccessible(true);
                        f.set(obj, value);
                    } catch (NoSuchFieldException e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return obj;
    }

    public static String sha1Sign(Map<String, String> map) {
        List<String> list = new ArrayList<>(map.keySet());
        Collections.sort(list);
        String keyValueStr = "";
        for (String key : list) {
            if (null != map.get(key) && !"sign".equals(key)) {
                if (StringUtils.isNotEmpty(keyValueStr)) {
                    keyValueStr += "&";
                }
                keyValueStr += key + "=" + map.get(key);
            }
        }
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            digest.reset();
            digest.update(keyValueStr.getBytes("UTF-8"));
            byte[] messageDigest = digest.digest();
            StringBuilder sb = new StringBuilder();
            for (byte b:
                    messageDigest) {
                String shaHex = Integer.toHexString(b & 0xFF);
                if (shaHex.length() < 2) {
                    sb.append(0);
                }
                sb.append(shaHex);
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String md5Sign(Map<String, String> map, String signKey) {
        List<String> list = new ArrayList<>(map.keySet());
        Collections.sort(list);
        String keyValueStr = "";
        for (String key : list) {
            if (null != map.get(key) && !"sign".equals(key)) {
                keyValueStr += key + "=" + map.get(key) + "&";
            }
        }
        keyValueStr = keyValueStr + "key=" + signKey;
        return MD5Util.paseMD5(keyValueStr).toUpperCase();
    }

}
